package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityVoucherData;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.vo.pyhandle.VoucherListVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoVoucherData;
import com.ocom.payhandle.feign.PayConfigClientService;
import com.ocom.payhandle.service.VoucherDataService;
import com.ocom.payhandle.service.XfRecordProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class VoucherDataServiceImpl extends ServiceImpl<IDaoVoucherData, EntityVoucherData> implements VoucherDataService {

    @Autowired
    IDaoVoucherData iDaoVoucherData;

    @Autowired
    XfRecordProcessService xfRecordProcessService;

    @Autowired
    PayConfigClientService payConfigClientService;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    public Boolean useVoucherNo(Long comId, String voucherNo, Integer num) {
        UpdateWrapper<EntityVoucherData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("com_id", comId);
        updateWrapper.eq("voucher_no", voucherNo);
        updateWrapper.eq("data_status", 4);
        updateWrapper.apply("used_number+" + num + "<=number");
        updateWrapper.setSql("used_number = used_number + " + num);
        return this.update(updateWrapper);
    }

    public List<VoucherListVo> selectVoucherList(Long comId,
                                                 Long pid,
                                                 Date consDate,
                                                 String VoucherTypes,
                                                 Integer consTimeZone,
                                                 Integer consType) {
        return iDaoVoucherData.selectVoucherList(comId, pid, consDate,VoucherTypes,consTimeZone,consType);
    }


    //通话套餐用
    public List<VoucherListVo> selectVoucherList(Long comId,
                                                 Long pid,
                                                 Date consDate) {
        return iDaoVoucherData.selectUserVoucherList(comId, pid, consDate);
    }

}
